package com.imooc.sell.controller;

import com.imooc.sell.dataobject.ProductCategory;
import com.imooc.sell.dataobject.ProductInfo;
import com.imooc.sell.service.CategoryService;
import com.imooc.sell.service.ProductService;
import com.imooc.sell.service.VO.ProductInfoVO;
import com.imooc.sell.service.VO.ProductVO;
import com.imooc.sell.service.VO.ResultVO;
import com.imooc.sell.utils.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 * 买家商品
 *
 * @author zzy
 * @create 2017-09-07 13:33
 */
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public ResultVO list() {

//    1.查询所有上架商品
        List<ProductInfo> productInfoList = productService.findUpAll();
        System.out.println(productInfoList);
//     2.查询类目（一次性查询）

//        传统方法
//        List<Integer> categroyTypeList = new ArrayList<>();
//        for (ProductInfo productInfo : productInfoList) {
//            categroyTypeList.add(productInfo.getCategoryType());
//        }
//

//        精简方法（java8,lambda）
        List<Integer> categroyTypeList = productInfoList.stream().map(e -> e.getCategoryType()).collect(Collectors.toList());
        List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categroyTypeList);

// 3.数据拼装
        List<ProductVO> productVOList = new ArrayList<>();
        for (ProductCategory productCategory : productCategoryList) {

            // 外层对象
            ProductVO productVO = new ProductVO();

            productVO.setCategroyType(productCategory.getCategoryType());
            productVO.setCategroyName(productCategory.getCategoryName());

            //遍历商品详情
            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for (ProductInfo productInfo : productInfoList) {
                //比较type相等才执行
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())) {
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    //把productInfo数据拷贝到productInfovo
                    BeanUtils.copyProperties(productInfo, productInfoVO);
                    System.out.println(productInfoVO + "==========================");
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);

            productVOList.add(productVO);
        }

//        ResultVO resultVO = new ResultVO();
//        resultVO.setData(productVOList);
//        resultVO.setCode(0);
//        resultVO.setMsg("成功");
//        return resultVO;


        return ResultVOUtil.success(productVOList);
    }

}